﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.SharePoint.Taxonomy;

namespace SPContrib.SharePoint.Taxonomy.Extensions
{
    /// <summary>
    /// Extensions for the TermStore class
    /// </summary>
    public static class TermStoreExtensions
    {
        /// <summary>
        /// Finds termset groups using a criteria
        /// </summary>
        /// <param name="criteria">criteria to meet</param>
        public static IEnumerable<Group> FindTermSetGroupsByCriteria(this TermStore instance, Predicate<Group> criteria)
        {
            return from g in instance.Groups
                   where criteria(g)
                   select g;
        }

        /// <summary>
        /// Finds termset groups by the name of the group
        /// </summary>
        /// <param name="name">the name of the group to find</param>
        public static IEnumerable<Group> FindTermSetGroupsByName(this TermStore instance, string name)
        {
            return instance.FindTermSetGroupsByCriteria(g => g.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
        }
    }
}
